<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>cache.js - Documentation</title>

    <script src="/static/js/jquery.min.js"></script>
    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>

    <!-- Enable responsive viewport -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link href="/static/css/normalize.css" rel="stylesheet">
    <link href="/static/css/main.css" rel="stylesheet">

    <!-- Custom styles -->
    <link href="/static/css/page.css?body=1" rel="stylesheet" type="text/css" media="all">
    <link href="/static/css/github.css" rel="stylesheet" type="text/css" media="all">
    <link href="/static/css/perfect-scrollbar.min.css" rel="stylesheet">
    <link href="/static/css/font-awesome.min.css" rel="stylesheet" type="text/css" media="all">

    <link type="text/css" rel="stylesheet" href="styles/ionicons.min.css">
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="/static/js/html5shiv.js"></script>
    <script src="/static/js/respond.min.js"></script>
    <![endif]-->

    <script src="/static/js/scrollspy.js"></script>
</head>
<body>
<link rel="import" href="/widget/header.html?__inline">
<div id="main" class="fix-sidebar">
    <div class="sidebar scrollbar ps-container jsdoc-nav">
      <h3>Namespaces</h3><ul><li><a href="fis.html">fis</a><ul class='methods'><li data-type='method'><a href="fis.html#.emit">emit</a></li><li data-type='method'><a href="fis.html#.on">on</a></li><li data-type='method'><a href="fis.html#.once">once</a></li><li data-type='method'><a href="fis.html#.plugin">plugin</a></li><li data-type='method'><a href="fis.html#.release">release</a></li><li data-type='method'><a href="fis.html#.removeAllListeners">removeAllListeners</a></li><li data-type='method'><a href="fis.html#.removeListener">removeListener</a></li><li data-type='method'><a href="fis.html#.require">require</a></li><li data-type='method'><a href="fis.html#.time">time</a></li></ul></li><li><a href="fis.cache.html">cache</a><ul class='methods'><li data-type='method'><a href="fis.cache.html#.clean">clean</a></li></ul></li><li><a href="fis.cli.html">cli</a><ul class='methods'><li data-type='method'><a href="fis.cli.html#.help">help</a></li><li data-type='method'><a href="fis.cli.html#.run">run</a></li><li data-type='method'><a href="fis.cli.html#.version">version</a></li></ul></li><li><a href="fis.compile.html">compile</a><ul class='methods'><li data-type='method'><a href="fis.compile.html#.analyseComment">analyseComment</a></li><li data-type='method'><a href="fis.compile.html#.clean">clean</a></li><li data-type='method'><a href="fis.compile.html#.extCss">extCss</a></li><li data-type='method'><a href="fis.compile.html#.extHtml">extHtml</a></li><li data-type='method'><a href="fis.compile.html#.extJs">extJs</a></li><li data-type='method'><a href="fis.compile.html#.isInline">isInline</a></li><li data-type='method'><a href="fis.compile.html#.partial">partial</a></li><li data-type='method'><a href="fis.compile.html#.process">process</a></li><li data-type='method'><a href="fis.compile.html#.setup">setup</a></li><li data-type='method'><a href="fis.compile.html#.xLang">xLang</a></li><li data-type='method'><a href="fis.compile.html#~builtinStandard">builtinStandard</a></li><li data-type='method'><a href="fis.compile.html#~pipe">pipe</a></li><li data-type='method'><a href="fis.compile.html#~postStandard">postStandard</a></li></ul></li><li><a href="fis.compile.lang.html">lang</a><ul class='methods'><li data-type='method'><a href="fis.compile.lang.html#.add">add</a></li></ul></li><li><a href="fis.config.html">config</a></li><li><a href="fis.emitter.html">emitter</a></li><li><a href="fis.file.html">file</a></li><li><a href="fis.log.html">log</a><ul class='methods'><li data-type='method'><a href="fis.log.html#.debug">debug</a></li><li data-type='method'><a href="fis.log.html#.error">error</a></li><li data-type='method'><a href="fis.log.html#.format">format</a></li><li data-type='method'><a href="fis.log.html#.info">info</a></li><li data-type='method'><a href="fis.log.html#.notice">notice</a></li><li data-type='method'><a href="fis.log.html#.now">now</a></li><li data-type='method'><a href="fis.log.html#.warn">warn</a></li><li data-type='method'><a href="fis.log.html#.warning">warning</a></li><li data-type='method'><a href="fis.log.html#~log">log</a></li></ul></li><li><a href="fis.project.html">project</a><ul class='methods'><li data-type='method'><a href="fis.project.html#.currentMedia">currentMedia</a></li><li data-type='method'><a href="fis.project.html#.getCachePath">getCachePath</a></li><li data-type='method'><a href="fis.project.html#.getProjectPath">getProjectPath</a></li><li data-type='method'><a href="fis.project.html#.getSource">getSource</a></li><li data-type='method'><a href="fis.project.html#.getTempPath">getTempPath</a></li><li data-type='method'><a href="fis.project.html#.lookup">lookup</a></li><li data-type='method'><a href="fis.project.html#.setProjectRoot">setProjectRoot</a></li><li data-type='method'><a href="fis.project.html#.setTempRoot">setTempRoot</a></li></ul></li><li><a href="fis.uri.html">uri</a></li><li><a href="fis.util.html">util</a><ul class='methods'><li data-type='method'><a href="fis.util.html#.applyMatches">applyMatches</a></li><li data-type='method'><a href="fis.util.html#.base64">base64</a></li><li data-type='method'><a href="fis.util.html#.camelcase">camelcase</a></li><li data-type='method'><a href="fis.util.html#.clone">clone</a></li><li data-type='method'><a href="fis.util.html#.copy">copy</a></li><li data-type='method'><a href="fis.util.html#.del">del</a></li><li data-type='method'><a href="fis.util.html#.download">download</a></li><li data-type='method'><a href="fis.util.html#.escapeReg">escapeReg</a></li><li data-type='method'><a href="fis.util.html#.escapeShellArg">escapeShellArg</a></li><li data-type='method'><a href="fis.util.html#.escapeShellCmd">escapeShellCmd</a></li><li data-type='method'><a href="fis.util.html#.exist">exist</a></li><li data-type='method'><a href="fis.util.html#.ext">ext</a></li><li data-type='method'><a href="fis.util.html#.filter">filter</a></li><li data-type='method'><a href="fis.util.html#.find">find</a></li><li data-type='method'><a href="fis.util.html#.getMimeType">getMimeType</a></li><li data-type='method'><a href="fis.util.html#.glob">glob</a></li><li data-type='method'><a href="fis.util.html#.install">install</a></li><li data-type='method'><a href="fis.util.html#.isAbsolute">isAbsolute</a></li><li data-type='method'><a href="fis.util.html#.isDir">isDir</a></li><li data-type='method'><a href="fis.util.html#.isEmpty">isEmpty</a></li><li data-type='method'><a href="fis.util.html#.isFile">isFile</a></li><li data-type='method'><a href="fis.util.html#.isImageFile">isImageFile</a></li><li data-type='method'><a href="fis.util.html#.isTextFile">isTextFile</a></li><li data-type='method'><a href="fis.util.html#.isUtf8">isUtf8</a></li><li data-type='method'><a href="fis.util.html#.isWin">isWin</a></li><li data-type='method'><a href="fis.util.html#.map">map</a></li><li data-type='method'><a href="fis.util.html#.md5">md5</a></li><li data-type='method'><a href="fis.util.html#.merge">merge</a></li><li data-type='method'><a href="fis.util.html#.mkdir">mkdir</a></li><li data-type='method'><a href="fis.util.html#.mtime">mtime</a></li><li data-type='method'><a href="fis.util.html#.nohup">nohup</a></li><li data-type='method'><a href="fis.util.html#.pad">pad</a></li><li data-type='method'><a href="fis.util.html#.parseUrl">parseUrl</a></li><li data-type='method'><a href="fis.util.html#.pathinfo">pathinfo</a></li><li data-type='method'><a href="fis.util.html#.pipe">pipe</a></li><li data-type='method'><a href="fis.util.html#.query">query</a></li><li data-type='method'><a href="fis.util.html#.read">read</a></li><li data-type='method'><a href="fis.util.html#.readBuffer">readBuffer</a></li><li data-type='method'><a href="fis.util.html#.readJson">readJson</a></li><li data-type='method'><a href="fis.util.html#.realpath">realpath</a></li><li data-type='method'><a href="fis.util.html#.realpathSafe">realpathSafe</a></li><li data-type='method'><a href="fis.util.html#.stringQuote">stringQuote</a></li><li data-type='method'><a href="fis.util.html#.toEncoding">toEncoding</a></li><li data-type='method'><a href="fis.util.html#.touch">touch</a></li><li data-type='method'><a href="fis.util.html#.upload">upload</a></li><li data-type='method'><a href="fis.util.html#.write">write</a></li></ul></li></ul><h3>Classes</h3><ul><li><a href="fis.cache-Cache.html">Cache</a><ul class='methods'><li data-type='method'><a href="fis.cache-Cache.html#addDeps">addDeps</a></li><li data-type='method'><a href="fis.cache-Cache.html#mergeDeps">mergeDeps</a></li><li data-type='method'><a href="fis.cache-Cache.html#removeDeps">removeDeps</a></li><li data-type='method'><a href="fis.cache-Cache.html#revert">revert</a></li><li data-type='method'><a href="fis.cache-Cache.html#save">save</a></li></ul></li><li><a href="fis.config.Config.html">Config</a><ul class='methods'><li data-type='method'><a href="fis.config.Config.html#del">del</a></li><li data-type='method'><a href="fis.config.Config.html#get">get</a></li><li data-type='method'><a href="fis.config.Config.html#getMatches">getMatches</a></li><li data-type='method'><a href="fis.config.Config.html#getSortedMatches">getSortedMatches</a></li><li data-type='method'><a href="fis.config.Config.html#hook">hook</a></li><li data-type='method'><a href="fis.config.Config.html#match">match</a></li><li data-type='method'><a href="fis.config.Config.html#media">media</a></li><li data-type='method'><a href="fis.config.Config.html#merge">merge</a></li><li data-type='method'><a href="fis.config.Config.html#set">set</a></li><li data-type='method'><a href="fis.config.Config.html#unhook">unhook</a></li></ul></li><li><a href="fis.file-File.html">File</a><ul class='methods'><li data-type='method'><a href="fis.file-File.html#addAsyncRequire">addAsyncRequire</a></li><li data-type='method'><a href="fis.file-File.html#addLink">addLink</a></li><li data-type='method'><a href="fis.file-File.html#addRequire">addRequire</a></li><li data-type='method'><a href="fis.file-File.html#addSameNameRequire">addSameNameRequire</a></li><li data-type='method'><a href="fis.file-File.html#defineLikes">defineLikes</a></li><li data-type='method'><a href="fis.file-File.html#exists">exists</a></li><li data-type='method'><a href="fis.file-File.html#getBase64">getBase64</a></li><li data-type='method'><a href="fis.file-File.html#getCacheData">getCacheData</a></li><li data-type='method'><a href="fis.file-File.html#getContent">getContent</a></li><li data-type='method'><a href="fis.file-File.html#getHash">getHash</a></li><li data-type='method'><a href="fis.file-File.html#getHashRelease">getHashRelease</a></li><li data-type='method'><a href="fis.file-File.html#getId">getId</a></li><li data-type='method'><a href="fis.file-File.html#getUrl">getUrl</a></li><li data-type='method'><a href="fis.file-File.html#isDir">isDir</a></li><li data-type='method'><a href="fis.file-File.html#isFile">isFile</a></li><li data-type='method'><a href="fis.file-File.html#isImage">isImage</a></li><li data-type='method'><a href="fis.file-File.html#isText">isText</a></li><li data-type='method'><a href="fis.file-File.html#removeAsyncRequire">removeAsyncRequire</a></li><li data-type='method'><a href="fis.file-File.html#removeRequire">removeRequire</a></li><li data-type='method'><a href="fis.file-File.html#revertFromCacheData">revertFromCacheData</a></li><li data-type='method'><a href="fis.file-File.html#setContent">setContent</a></li><li data-type='method'><a href="fis.file-File.html#toString">toString</a></li></ul></li></ul><h3>Events</h3><ul><li><a href="fis.html#.event:lookup:file">lookup:file</a></li></ul>
    </div>

    <div class="content api with-sidebar">
        
        <h1 class="page-title">cache.js</h1>
        

        



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>'use strict';
/**
 * Cache 构造器，在 fis 中主要用于缓存目标文件的编译信息和编译结果。
 *
 * @param {String} path 需要被缓存的文件路径。
 * @param {String} dir 缓存目录。
 * @inner
 * @class
 * @memberOf fis.cache
 */
var Cache = Object.derive(function(path, dir) {
  var file = fis.util.realpath(path);
  if (!fis.util.isFile(file)) {
    fis.log.error('unable to cache file[%s]: No such file.', path);
  }
  this.timestamp = fis.util.mtime(file).getTime();
  this.deps = {};
  this.version = fis.version;

  var info = fis.util.pathinfo(file);
  var basename = fis.project.getCachePath(dir, info.basename);
  var hash = fis.util.md5(file, 10);
  this.cacheFile = basename + '-c-' + hash + '.tmp';
  this.cacheInfo = basename + '-o-' + hash + '.json';
},

  /** @lends fis.cache~Cache.prototype */
  {

  /**
   * 保存内容以及信息。
   * @param  {String | Buffer} content 文件内容
   * @param  {Object} info 数据信息。
   */
  save: function(content, info) {
    var infos = {
      version: this.version,
      timestamp: this.timestamp,
      deps: this.deps,
      info: info
    };
    fis.util.write(this.cacheInfo, JSON.stringify(infos));
    fis.util.write(this.cacheFile, content);
  },

  /**
   * 从缓存目录中读取缓存内容。
   * @param  {Object} [file] 如果传入了此参数，从缓存文件中读取的内容将会赋值到 `file.content` 以及数据信息会被赋值到 `file.info`.
   * @return {Boolean} 返回 true, 如果缓存有效，否则返回 false.
   */
  revert: function(file) {
    fis.log.debug('revert cache');
    if (
      exports.enable &amp;&amp; fis.util.exists(this.cacheInfo) &amp;&amp; fis.util.exists(this.cacheFile)
    ) {
      fis.log.debug('cache file exists');
      var infos = fis.util.readJSON(this.cacheInfo);
      fis.log.debug('cache info read');
      if (infos.version == this.version &amp;&amp; infos.timestamp == this.timestamp) {
        var deps = infos['deps'];
        for (var f in deps) {
          if (deps.hasOwnProperty(f)) {
            var d = fis.util.mtime(f);
            if (d == 0 || deps[f] != d.getTime()) {
              fis.log.debug('cache is expired');
              return false;
            }
          }
        }
        this.deps = deps;
        fis.log.debug('cache is valid');
        if (file) {
          file.info = infos.info;
          file.content = fis.util.fs.readFileSync(this.cacheFile);
        }
        fis.log.debug('revert cache finished');
        return true;
      }
    }
    fis.log.debug('cache is expired');
    return false;
  },

  /**
   * 添加依赖，依赖将会被用来判断缓存是否有效，依赖中，任何一个文件修改时间发生变化，缓存失效。
   * @param {String} filepath 依赖的文件路径。
   */
  addDeps: function(file) {
    var path = fis.util.realpath(file);
    if (path) {
      this.deps[path] = fis.util.mtime(path).getTime();
    } else {
      fis.log.warning('unable to add dependency file[' + file + ']: No such file.');
    }
    return this;
  },

  /**
   * 删除依赖。
   * @param {String} filepath 依赖的文件路径。
   */
  removeDeps: function(file) {
    var path = fis.util.realpath(file);
    if (path &amp;&amp; this.deps[path]) {
      delete this.deps[path];
    }
    return this;
  },

  /**
   * 合并 cache 中的依赖列表。
   * @param {mixed} cache 此对象中的依赖会被合入到该事例依赖中。
   */
  mergeDeps: function(cache) {
    var deps = {};
    if (cache instanceof Cache) {
      deps = cache.deps;
    } else if (typeof cache === 'object') {
      deps = cache;
    } else {
      fis.log.error('unable to merge deps of data[%s]', cache);
    }
    fis.util.map(deps, this.deps, true);
  }
});

/**
 * 用来创建 ~Cache 对象， 更多细节请查看 {@link fis.cache~Cache ~Cache} 说明。
 *
 * @example
 * // 检查该文件是否启用缓存
 * if (file.useCache) {
 *
 *   // 如果启用，则创建 cache 对象
 *   file.cache = fis.cache(file.realpath, CACHE_DIR);
 *
 *   // 保存内容以及信息到缓存中。
 *   file.cache.save('body', {
 *     foo: 1
 *   });
 * }
 * @see {@link fis.cache~Cache Cache 类说明}
 * @param {String} path 需要被缓存的文件路径。
 * @param {String} dir 缓存目录。
 * @function
 * @namespace fis.cache
 */
var exports = module.exports = Cache.factory();

/**
 * 是否开启缓存。当设置为 false 后, fis 编译将不会启用缓存。
 * @type {Boolean}
 * @defaultValue true
 * @memberOf fis.cache
 * @name enable
 */
exports.enable = true;

/**
 * 指向 {@link fis.cache~Cache ~Cache 类}。
 * @see {@link fis.cache~Cache Cache 类说明}
 * @memberOf fis.cache
 * @name Cache
 */
exports.Cache = Cache;

/**
 * 清除缓存目录下面指定的目录。
 * @example
 *
 * // 当命令行中设置了 --clean 属性后，开始之前，先清除之前编译产生的缓存文件。
 * if (options.clean) {
 *   fis.cache.clean('compile');
 * }
 * @param  {String} name 需要清除的目录名
 * @memberOf fis.cache
 * @name clean
 * @function
 */
exports.clean = function(name) {
  name = name || '';
  var path = fis.project.getCachePath(name);
  if (fis.util.exists(path)) {
    fis.util.del(path);
  }
};
</code></pre>
        </article>
    </section>




    </div>
</div>

<footer>
  <div class="footer-inner">
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a> on Fri Jul 03 2015 16:22:12 GMT+0800 (CST) using the Minami theme.
    </div>
</footer>

<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
<!-- Latest compiled and minified JavaScript, requires jQuery 1.x (2.x not supported in IE8) -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="/static/js/perfect-scrollbar.min.js"></script>
<script type="text/javascript">
    (function(){
        var scrollBarEnabled = false;

        function isIE( version, comparison ){
            var $div = $('<div style="display:none;"/>').appendTo($('body'));
            $div.html('<!--[if '+(comparison||'')+' IE '+(version||'')+']><a>&nbsp;</a><![endif]-->');
            var ieTest = $div.find('a').length;
            $div.remove();
            return ieTest;
        }

        function updateScrollBar(){
            function enableScrollBar(){
                if (scrollBarEnabled)
                    return;
                scrollBarEnabled = true;
                $(".sidebar").perfectScrollbar({
                    wheelSpeed: 10,
                    wheelPropagation: false
                });
            }

            function disableScrollBar(){
                if (!scrollBarEnabled)
                    return;
                scrollBarEnabled = false;
                $(".sidebar").perfectScrollbar('destroy');
            }

            if ($(window).width()>760 && $(".sidebar").height() < $(".sidebar ul").height()){
                enableScrollBar();
            }else{
                disableScrollBar();
            }
        }

        //设置链接当前样式
        function setActive(){
            var href= window.location.href;
            $(".sidebar li a").each(function(){
                var link = $(this).attr("href");
                if(String(href).indexOf(link) > -1){
                    $(this).addClass("active");
                }
            })
        }

        //生成左边导航用于在移动端查看使用
        function buildSideNav(){
            $("nav .nav-left").clone().attr("class","main-nav").prependTo($(".sidebar"));

            $(".header-wrap .toggle").addClass("fa-list").on("click",function(){
                $(".sidebar").toggleClass("open");
                return false;
            })

            $("html").on("click",function(e){
                console.log(e.target);
                $(".sidebar").removeClass("open");
            })

            $(".sidebar").bind("click",function(e){
                e && e.stopPropagation();
            })
        }

        //hash平滑滚动，a链接添加active
        $('.sidebar li a').click(function(){
            $('.sidebar li a').removeClass("active");
            $(this).addClass("active");
            $(".sidebar").removeClass("open");
        });

        setActive();
        buildSideNav();


        if (!isIE(8, 'lte')){
            $(".sidebar").addClass('scrollbar');
            updateScrollBar();
            $(window).resize(updateScrollBar);
        }
    }());
</script>
</body>
</html>
